Fix integration tests with same names as libs
authorAlex Crichton <alex@alexcrichton.com>
Wed, 23 Jul 2014 18:44:15 +0000 (11:44 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 23 Jul 2014 18:44:15 +0000 (11:44 -0700)
Use extra-filename mixins to ensure that these names never clash.

src/cargo/core/manifest.rs
src/cargo/core/package_id.rs
src/cargo/util/toml.rs
tests/test_cargo_test.rs

index 8d148e04770753af23436c1a78f79e014dc496e7..301831fc084618268d295caf0b76c6a2d02c4e1a 100644 (file)
@@ -351,13 +351,14 @@ impl Target {
         }
     }
 
-    pub fn test_target(name: &str, src_path: &Path, profile: &Profile) -> Target {
+    pub fn test_target(name: &str, src_path: &Path,
+                       profile: &Profile, metadata: Metadata) -> Target {
         Target {
             kind: BinTarget,
             name: name.to_string(),
             src_path: src_path.clone(),
             profile: profile.clone(),
-            metadata: None
+            metadata: Some(metadata),
         }
     }
 
index 51ce421d2909306c6194398cbfc4ccb0b7440643..02bf6fc2906f94d8a14fda4afc820074013e5ce0 100644 (file)
@@ -126,6 +126,14 @@ impl PackageId {
     }
 }
 
+impl Metadata {
+    pub fn mix<T: Hash>(&mut self, t: &T) {
+        let new_metadata = short_hash(&(self.metadata.as_slice(), t));
+        self.extra_filename = format!("-{}", new_metadata);
+        self.metadata = new_metadata;
+    }
+}
+
 static central_repo: &'static str = "http://rust-lang.org/central-repo";
 
 impl Show for PackageId {
index 40607e93700a6fbf2ad0e27ed926f76af3c83326..1f9f1ffb01e6227d25daa00f90f72791c12ae8af 100644 (file)
@@ -559,13 +559,18 @@ fn normalize(libs: &[TomlLibTarget],
     }
 
     fn test_targets(dst: &mut Vec<Target>, tests: &[TomlTestTarget],
-                   default: |&TomlTestTarget| -> String) {
+                    metadata: &Metadata,
+                    default: |&TomlTestTarget| -> String) {
         for test in tests.iter() {
             let path = test.path.clone().unwrap_or_else(|| {
                 TomlString(default(test))
             });
 
             let profile = &Profile::default_test();
+            // make sure this metadata is different from any same-named libs.
+            let mut metadata = metadata.clone();
+            metadata.mix(&format!("test-{}", test.name.as_slice()));
+
             dst.push(Target::test_target(test.name.as_slice(),
                                          &path.to_path(),
                                          profile,
index 769c8238196509b6dd7353fdc70b9df522065c7f..910f0437f147d70237f11262b42320d6b3d17ffb 100644 (file)
@@ -42,6 +42,35 @@ test!(cargo_test_simple {
     assert_that(&p.bin("test/foo"), existing_file());
 })
 
+test!(many_similar_names {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "
+            pub fn foo() {}
+            #[test] fn lib_test() {}
+        ")
+        .file("src/main.rs", "
+            extern crate foo;
+            fn main() {}
+            #[test] fn bin_test() { foo::foo() }
+        ")
+        .file("tests/foo.rs", r#"
+            extern crate foo;
+            #[test] fn test_test() { foo::foo() }
+        "#);
+
+    let output = p.cargo_process("cargo-test").exec_with_output().assert();
+    let output = str::from_utf8(output.output.as_slice()).assert();
+    assert!(output.contains("test bin_test"), "bin_test missing\n{}", output);
+    assert!(output.contains("test lib_test"), "lib_test missing\n{}", output);
+    assert!(output.contains("test test_test"), "test_test missing\n{}", output);
+})
+
 test!(cargo_test_failing_test {
     let p = project("foo")
         .file("Cargo.toml", basic_bin_manifest("foo").as_slice())